package com.qf.service.impl;


import com.qf.dao.UserDao;
import com.qf.pojo.User;
import com.qf.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class UserServiceImpl implements UserService {

    private static UserDao userDao;

    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

    @Override
    public void regist(User user) {
        boolean b = checkUsername(user.getUsername());
        if (b){
            userDao.insert(user);
        }else{
            throw new RuntimeException("用户名已存在");
        }

    }

    @Override
    public boolean checkUsername(String username){
        User user=userDao.selectByUsername(username);
        if (user==null){
            return true;
        }
        return false;
    }

    @Override
    public User login(String username, String password) {
        User user=userDao.selectUser(username,password);
        if (user!=null){
            return user;
        }else {
            throw new RuntimeException("用户名或密码不正确");
        }
    }

}
